package org.demo.kafka.conteoller;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.demo.datasoureces.entity.UserInfo;
import org.demo.datasoureces.service.UserInfoService;
import org.demo.kafka.producer.KafkaProducer;
import org.demo.localcache.entity.User;
import org.demo.localcache.guava.LocalDemoCache;
import org.demo.redis.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@Slf4j
@RequestMapping("kafka")
public class KafkaController {

    @Autowired
    KafkaProducer kafkaProducer;

    @Autowired
    LocalDemoCache localDemoCache;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    UserInfoService userInfoService;

    @RequestMapping("setMsg")
    @ResponseBody
    public boolean setMsg(@RequestParam String message){
        kafkaProducer.sendMsg(message);
        return true;
    }

    @RequestMapping("setMsgInfo")
    @ResponseBody
    public boolean setMsgInfo(){
        UserInfo userInfo=userInfoService.queryUserInfoById(userInfoService.queryMaxId()+0l);
        kafkaProducer.sendMsg(JSONObject.toJSONString(userInfo));
        return true;
    }

    @RequestMapping("getmsgLocal")
    @ResponseBody
    public User getMsgLocal(){
        return localDemoCache.get("");
    }

    @RequestMapping("getmsgRedis")
    @ResponseBody
    public Object getMsgRedis(@RequestParam String key){
        return redisUtils.get(key);
    }


}
